Examine the following code:
String userData = " 745 "; String fixed; fixed = userData.trim();
trim()
method been used correctly? Yes
trim()
Method
The trim()
method
creates a new String.
The new String contains
the same characters as the old one
but has
any whitespace characters
(blanks, tabs, and several other non-printing characters)
removed from both ends (but not from the middle).
So, for example, after the last statement. . .
String userData = " 745 "; String fixed; fixed = userData.trim();
. . .the new String referenced by fixed
will contain the characters "745" but not
the surrounding spaces.
(Usage Note:) This method is very useful. Extraneous spaces on either end of user input is a common problem.